﻿using IQKS.AWP.BusinessAccessLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Candidate_CandidateInvMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSearchRegId_Click(object sender, EventArgs e)
    {
        load_CanddidateData();
    }
    private void load_CanddidateData()
    { 
        string strRegNum = Convert.ToString(txtRegNum.Text.Trim());
        DataSet objDs;
        lblErrorMsg.Visible = false;
        gvByRegID.Visible = false;
        try
        {
            objDs = new UserMasterBAL().GetCandidateDetailsByRegNum(strRegNum);
            if (objDs.Tables[0].Rows.Count == 0)
            {
                lblErrorMsg.Text = "Record Not Found..";
                lblErrorMsg.Visible = true;
            }
            else {
                gvByRegID.Visible = true;
                gvByRegID.Columns[0].Visible = true;
                gvByRegID.DataSource = objDs;
                gvByRegID.DataBind();
                gvByRegID.Columns[0].Visible = false;
                            
                //ViewState["strUserId"] = Convert.ToString(objDs.Tables[0].Rows[0]["UserId"]);
                //ViewState["strCid"] = Convert.ToString(objDs.Tables[0].Rows[0]["CandidateID"]);

            }


        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
    }


    protected void gvByRegID_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "BtnInvGenerate")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            CandidateDetailsBAL cbal = new CandidateDetailsBAL();
            GridViewRow row = gvByRegID.Rows[index];
            int vCandidateId = row.Cells[0].Text.ToInt();
            if (vCandidateId > 0 && (String.IsNullOrEmpty(row.Cells[4].Text) || row.Cells[4].Text == "&nbsp;"))
            {
                String strReturnValue;
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Candidate [" + row.Cells[1].Text + "] Invoice Generating..');", true);               
                strReturnValue = (string)cbal.SetCandidateInvoice(vCandidateId, "GBP", "£");
                if (strReturnValue == "SUCCESS")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invoice generated succesfully.');", true);
                    load_CanddidateData();                    
                }
                else if (strReturnValue == "EXIST")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Some error in Invoice generation.');", true);                    
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                }
            }
            else if (vCandidateId == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error in grid data.');", true);
            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invoice no [" + row.Cells[4].Text + "] allready generated..');", true);

            // Load report
            if (vCandidateId > 0 && (String.IsNullOrEmpty(row.Cells[4].Text) || row.Cells[4].Text == "&nbsp;"))
            { }
            else
                Response.Redirect("~/Reports/CandidateInvoiceView.aspx?CandidateId=" + vCandidateId);
        }
    }
}